package cn.koala.key.stock.controller;

import cn.koala.key.common.excel.ExcelUtils;
import cn.koala.key.entity.KeyInfo;
import cn.koala.key.stock.domain.vo.EnterStockDetailVO;
import cn.koala.key.stock.domain.vo.NewCarKeyInfoExcel;
import cn.koala.key.stock.service.EnterStockNewCarService;
import cn.ehi.authority.annotation.WebAuthentication;
import cn.seed.common.core.Result;
import cn.seed.common.utils.LoggerUtils;
import com.alibaba.excel.support.ExcelTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @description:新车入库
 * @author 32415
 * @time 2019/04/28
 **/
@RestController
@Api(value = "EnterStockNewCar",tags = "新车入库")
public class EnterStockNewCarController {

    @Autowired
    private EnterStockNewCarService enterStockNewCarService;

    @ApiOperation(notes = "批量导入新车", value = "批量导入新车")
    @PostMapping("/new-car/file")
    public Result importNewCar(@RequestParam MultipartFile newCarFile){
        return enterStockNewCarService.importNewCar(newCarFile);
    }

    @ApiOperation(value = "提交")
    @PutMapping("/new-car/submit")
    @WebAuthentication(systemCode = "KoalaKey",moduleIds = "KOALAKEY_B_00018")
    public Result submit(@RequestParam String orderNo) {
        return enterStockNewCarService.submit(orderNo);
    }

    @ApiOperation(value = "作废")
    @PutMapping("/new-car/invalid")
    @WebAuthentication(systemCode = "KoalaKey",moduleIds = "KOALAKEY_B_00016")
    public Result invalid(@RequestParam String orderNo) {
        return enterStockNewCarService.invalid(orderNo);
    }

    @ApiOperation(value = "删除库存钥匙")
    @PutMapping("/new-car/delete")
    public Result delete(@RequestParam String keyBarCode) {
        return enterStockNewCarService.delete(keyBarCode);
    }

    @ApiOperation(value = "详情")
    @GetMapping("/new-car")
    @WebAuthentication(systemCode = "KoalaKey",moduleIds = "KOALAKEY_B_00012")
    public Result<EnterStockDetailVO> selectNewCarDetail(@RequestParam String orderNo) {
        return enterStockNewCarService.selectNewCarDetail(orderNo);
    }

    @ApiOperation(value = "根据单号获取钥匙信息")
    @GetMapping("/new-car/getKeyBarCode")
    public Result<List<KeyInfo>> selectNewCarKeyInfo(@RequestParam String orderNo) {
        return enterStockNewCarService.selectNewCarKeyInfo(orderNo);
    }

    @ApiOperation(value = "新车车辆信息导出")
    @PostMapping("/new-car/print")
    public void exportNewCarKeyInfo(HttpServletResponse response,
                                     @RequestParam String orderNo){
        Result<List<KeyInfo>> list = enterStockNewCarService.selectNewCarKeyInfo(orderNo);
        List<KeyInfo> data = list.getResult();
        List<NewCarKeyInfoExcel> resultList = enterStockNewCarService.dataProcessing(data);
        //导出操作
        try {
            ExcelUtils.setResponse(response,"新车车辆信息");
            ExcelUtils.export(response.getOutputStream(),ExcelTypeEnum.XLSX,resultList,NewCarKeyInfoExcel.class);
        } catch (Exception e) {
            LoggerUtils.error(EnterStockNewCarController.class, e.getMessage());
        }
    }

}
